
<html>
<head><title>CS 143 _ Project #1A - CALCULATOR</title></head>
<body style="background-color:#FFA500;text-align = center">

<h1>CS 143 - Project #1A: CALCULATOR</h1>
<p>            (Phuc Nguyen)</p>
<br />
<p>ENTER AN EXPRESSION TO CALCULATE: (Ex: 2.7-1.3*6.9/3 -> Submit).</p>
<p>
<form action="calculator.php" method="GET">
 Expression = <input type="text" name="expr">
<input type="submit" value="<<SUBMIT>>"> <br />
 Result     =
</form>
</p>
<h2 style="text-color = Red">
 
<?php
 
$input = $_GET["expr"];

$input = ereg_replace('[[:blank:]]','', $input);  /* Remove whitespaces*/
echo $input;
$input = ereg_replace('(\.)*\-\-(\.)*','+',$input);  /* change -- if any to +*/

function typeCheck ($input) {
	if ($input =="") {
		echo "Oh oh! Empty expr!";
		return $input;
	}
		
	if (!ereg('^\-?[0-9]+\.?[0-9]*([+/\*\-]\-?[0-9]+\.?[0-9]*)*$', $input)) {
 		echo "<p>Oh oh: Invalid input!</p>";
		exit;
	}
	elseif (!ereg('(\.)*/\-?[0]+\.?[0]*[+/\*\-](\.)*$', $input)) {
		echo "<p>Oh oh: Divide by zero?</p>";
		exit;
	}
	else       return $input;
}

function evaluate ($input) {
	if ($input =="") 
		return $input;
	eval('$output = '.$input.';');
	echo " = ".$output;
}


$input = typeCheck ($input);

evaluate($input);

?>
</h2>
<p  style = "text-align = left">
*** PLEASE NOTE: THIS CALCULATOR ONLY SUPPORTS:</p>
<ul style = "text-align = left">
<li>Operands types	: Integer or Float 
<li>Operations		: +,-,*,/ 
<li>Evaluation		: Standard operations precedence, and no brackets allowed.
<li>Error		: Not show in the box
</ul>
</body>
</html>

